package org.projectwithoutname.handler;
import java.util.ArrayList;

import model.Entity;
import model.SeasonsPackSeason;
import model.Serie;
import model.Workspace;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.projectwithoutname.ui.pages.MainPage;
import org.projectwithoutname.ui.pages.SeasonPage;
import org.projectwithoutname.ui.pages.SinopsePage;
import org.projectwithoutname.ui.pages.input.EntityEditorInput;
import org.projectwithoutname.ui.util.Hide;
import org.projectwithoutname.ui.view.EpisodesView;
import org.projectwithoutname.ui.view.InformationView;
import org.projectwithoutname.ui.view.SuperView;


@SuppressWarnings("restriction")
public class CallEditorHandler extends AbstractHandler implements IHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// Get the view
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		MainPage mainPage = (MainPage) page.findView(MainPage.ID);
		Hide manager = mainPage.getHide();
		Workspace workspace = mainPage.root;
		IPerspectiveRegistry registry = WorkbenchPlugin.getDefault().getPerspectiveRegistry();
		
//		create input to open the editors
		
		ArrayList<EntityEditorInput> seasons = new ArrayList<EntityEditorInput>();
		
//		Get the selection
		if (mainPage.getSelection() != null){
			Serie serie = (Serie) mainPage.getSelection();

			EntityEditorInput inputSinopse = new EntityEditorInput(serie);
				
			for (SeasonsPackSeason season : serie.getAllSeason().getAggregatedSeason()){
				EntityEditorInput input = new EntityEditorInput(season.getSeason());
				seasons.add(input);
			}
			
//				EntityEditorInput inputActors = new EntityEditorInput((AssociationActorSerie)serie.getSerieActor());
//				EntityEditorInput inputRoles = new EntityEditorInput(serie);
			
			try {
				IPerspectiveDescriptor perspective = registry.findPerspectiveWithId("org.ProjectWithoutName.application.seriePerspective");
				if (perspective != null){
//						closing perspective
					IPerspectiveDescriptor toClose = registry.findPerspectiveWithId(page.getPerspective().getId());
					manager.backAct(page.getPerspective().getId());
					page.closePerspective(toClose, true, false);
					
//						opening perspective
					String toNotify = perspective.getDescription();
					page.setPerspective(perspective);
					SuperView view = (SuperView) page.findView(toNotify);
					view.notify(manager);
					view.notify(workspace);
					view.notify(serie);
					if (view instanceof EpisodesView)
						((EpisodesView)view).initView();
					InformationView informationView = (InformationView) page.findView(InformationView.ID);
					informationView.showImageLayout();
					informationView.notify((Entity)serie);
					
				}
//					opening abas
				
					page.openEditor(inputSinopse, SinopsePage.ID);
				
				for (EntityEditorInput inputseason : seasons){
					page.openEditor(inputseason, SeasonPage.ID);
				}
//					page.openEditor(inputRoles, SeriePage.ID);
//					
//					page.openEditor(inputActors, SeriePage.ID);
				
			
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

}
